//
//  AppDelegate.swift
//  weibo
//
//  Created by apple on 16/2/17.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow?
    
    
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        //设置全局控件颜色
        setThemeColor()
        //添加通知
        registerNotification()
        
        //实例化window
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        //可视化
        window?.makeKeyAndVisible()
        
        //设置根控制器
        window?.rootViewController = defaultRootViewController()
        return true
    }
    //设置主体颜色（所有相同控件的颜色）
    private func setThemeColor() {
        UINavigationBar.appearance().tintColor = themeColor
        UITabBar.appearance().tintColor = themeColor

    }

    @objc private func AppRootViewController(n: NSNotification) {
    
        window?.rootViewController = n.object == nil ? WelcomeViewController() : MainViewController()
    }
    //注册通知
    private func registerNotification() {
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "AppRootViewController:", name: KChoseRootVC, object: nil)
        
    }
    //判断用户是否登录
    private func defaultRootViewController() -> UIViewController {
        return AccountViewModel.sharedAccountViewModel.userLogin ? WelcomeViewController() : MainViewController()
    }
    //移除通知 在添加通知后 要记得移除通知 即便移除的代码没用
    //代理 对象是一个单例对象
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
}

